﻿/**
* @file qomain.cpp
* @describe Qo主程序
* @author gaoyuelong
* @date 2021/11/23
*/

#include "qousercontrol.h"
#include "qousermanager.h"
#include "qomain.h"
#include "ui_qomain.h"
#include "common.h"
#include <QPixmap>
#include <QStringListModel>
#include <QCloseEvent>
#include <QMessageBox>
#include <QDesktopWidget>
#include <QVariant>
#include <QMenu>

const int MESSAGES_INDEX = 0;
const int FRIENDS_INDEX = 1;

QoMain::QoMain(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QoMain)
{
    ui->setupUi(this);
    //默认只能最小化和关闭
    setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
    init();
}

QoMain::~QoMain()
{
    delete m_pTrayIcon;
    m_pTrayIcon = nullptr;
    delete ui;
}

void QoMain::init()
{
    //设置默认头像
    QPixmap pixmapPhoto(":/new/prefix/images/defaultPhoto/default.png");
    ui->lbPhoto->setPixmap(pixmapPhoto);
    //设置昵称
    ui->lbName->setText(QoUserManager::getInstance()->getCurrentUser()->getNickName());

    m_pTrayIcon = new QSystemTrayIcon();
    m_pTrayIcon->setIcon(QIcon(":/new/prefix/images/qo.png"));
    m_pTrayIcon->show();

    connect(m_pTrayIcon, &QSystemTrayIcon::activated, this, &QoMain::iconActivated);

    initFriendsList();

    //搜索
    ui->lineeditSearch->addAction(QIcon(":/new/prefix/images/search.png"), QLineEdit::LeadingPosition);
}

//图标右下角
void QoMain::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::DoubleClick:
        show();
    case QSystemTrayIcon::MiddleClick:
        break;
    default:
        ;
    }
}

//当点击消息
void QoMain::on_btnMessages_clicked()
{
    ui->stackedWidget->setCurrentIndex(MESSAGES_INDEX);
    initMessagesList();
}

//当点击联系人
void QoMain::on_btnFriends_clicked()
{
    ui->stackedWidget->setCurrentIndex(FRIENDS_INDEX);
}

void QoMain::initMessagesList()
{
//    QStringList list;
//    list.push_back("hello");
//    QStringListModel *qstringMode = new QStringListModel(list);
    //ui->treeWgtMessages->setModel(qstringMode);
}

void QoMain::initFriendsList()
{
    m_pTreeWgtItemGroup = new QTreeWidgetItem(ui->treeWgtFriends);
    m_pTreeWgtItemGroup->setText(0, QStringLiteral("群组"));
    m_pTreeWgtItemFriends = new QTreeWidgetItem(ui->treeWgtFriends);
    m_pTreeWgtItemFriends->setText(0, QStringLiteral("好友"));
    m_pTreeWgtItemFriends->setExpanded(true);


    QVector<QoUser> vecQoUsers = QoUserManager::getInstance()->getCurrentUserFriends();

    //更新好友列表
    for (int i = 0; i < vecQoUsers.size(); i++) {
        QTreeWidgetItem *pTreeWgtItem = new QTreeWidgetItem(m_pTreeWgtItemFriends);
        pTreeWgtItem->setText(0, vecQoUsers[i].getNickName());
        pTreeWgtItem->setData(0, Qt::UserRole + 1, vecQoUsers[i].getAccount());

        QIcon icon;
        if (vecQoUsers[i].getState() == strOnLine) {
            icon.addFile(":/new/prefix/images/online.png");
        } else {
            icon.addFile(":/new/prefix/images/offline.png");
        }
        pTreeWgtItem->setIcon(0, icon);
    }

    //更新群组列表
//    QTreeWidgetItem *qtreewgtcapt2 = new QTreeWidgetItem(m_pTreeWgtItemFriends);
//    qtreewgtcapt2->setText(0, "capter2");
}

void QoMain::closeEvent(QCloseEvent *pEvent)
{
    //最小化还是关闭？
    int ret;
    if ((ret = QMessageBox::information(this, "tips", QStringLiteral("close?"), QMessageBox::Ok))
            == QMessageBox::Ok) {
        close();
        return;
    }
    hide();
    pEvent->ignore();
}

void QoMain::on_treeWgtFriends_itemDoubleClicked(QTreeWidgetItem *pItem, int column)
{
    if (pItem == m_pTreeWgtItemGroup || pItem == m_pTreeWgtItemFriends) return;

    QoChatWidget *pChatwidget = new QoChatWidget();

    //显示在屏幕中央
    QRect rect = QApplication::desktop()->screenGeometry(this);
    pChatwidget->move((rect.width() - pChatwidget->width()) / 2, (rect.height() - pChatwidget->height()) / 2);

    pChatwidget->setToAccount(pItem->data(0, Qt::UserRole + 1).toString());
    pChatwidget->show();
}
#include <QDebug>
void QoMain::on_btnOption_clicked()
{
    QMenu menu;

    menu.addAction(QIcon(":/new/prefix/images/set.png"), QStringLiteral("设置"), this, SLOT(showSettingPage()));
    menu.addAction(QIcon(":/new/prefix/images/quit.png"), QStringLiteral("退出"), this, SLOT(quitQo()));

    //sizeHint This property holds the recommended size for the widget
    QPoint point = QPoint(0,
        ui->widget_2->geometry().y() - menu.sizeHint().height());

    menu.exec(this->mapToGlobal(point));
}

void QoMain::on_btnAddFirend_clicked()
{

}

void QoMain::showSettingPage()
{

}

void QoMain::quitQo()
{

}

